package com.baic.ibase.service.permissions.impl;

import com.baic.ibase.mapper.RolesPermissionsMapper;
import com.baic.ibase.models.dao.RolesPermissions;
import com.baic.ibase.models.req.common.ApiResponse;
import com.baic.ibase.models.req.common.PageResponse;
import com.baic.ibase.models.req.roles.CreateRolesPermissionsReq;
import com.baic.ibase.models.req.roles.QueryRolesPermissionsPage;
import com.baic.ibase.models.resp.QueryRolesPermissionsResp;
import com.baic.ibase.service.permissions.RolesPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class RolesPermissionsServiceImpl extends ServiceImpl<RolesPermissionsMapper, RolesPermissions> implements RolesPermissionService {


    @Transactional
    @Override
    public ApiResponse<Integer> createRolesPermissions(CreateRolesPermissionsReq req) {
        try {
            List<RolesPermissions> rolesPermissions = new ArrayList<>();
            for (Long permissionId : req.getPermissionIds()) {
                RolesPermissions rolesPermission = new RolesPermissions();
                rolesPermission.setRolesId(req.getRoleId());
                rolesPermission.setPermissionId(permissionId);
                rolesPermission.setCreatedTime(LocalDateTime.now());
                rolesPermission.setUpdatedTime(LocalDateTime.now());
                // TODO: 获取当前用户ID
                rolesPermission.setCreatedUserId(1L);
                rolesPermission.setUpdatedUserId(1L);
                rolesPermissions.add(rolesPermission);
            }
            ApiResponse<Integer> response = new ApiResponse<>();
            if (this.saveBatch(rolesPermissions)) {
                response.setData(rolesPermissions.size());
                return response;
            } else {
                return ApiResponse.error(500, "添加角色权限失败",  null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(500, "添加角色权限失败", e.getMessage());
        }
    }

    @Override
    public ApiResponse<PageResponse<QueryRolesPermissionsResp>> listRolesPermissions(QueryRolesPermissionsPage req) {

        List<QueryRolesPermissionsResp> rolesPermissions = this.baseMapper.listRolesPermissions(req.getRoleId(), (req.getPageNum() - 1)* req.getPageSize(), req.getPageSize());
        PageResponse<QueryRolesPermissionsResp> response = new PageResponse<>();
        response.setData(rolesPermissions);
        response.setPageNum(req.getPageNum());
        response.setPageSize(req.getPageSize());
        response.setTotal(this.baseMapper.countRolesPermissions(req.getRoleId()));
        response.setPages((int) (response.getTotal() % req.getPageSize() == 0 ? response.getTotal() / req.getPageSize() : response.getTotal() / req.getPageSize() + 1));

        return ApiResponse.ok(response);
    }
}
